﻿using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.IO.Packaging;
using System.Text;
using System.Xml;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using OpenLS.Spreadsheet.Serialization;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    class MetroBlob
    {
        internal AnchorWrapper AnchorWrapper { get; set; }
        DownRevStg DownRevStg = new DownRevStg();
        public void WritePackage(Package package)
        {
            {
                var anchorUri = new Uri("/drs/downrev.xml", UriKind.Relative);
                PackageRelationship rcp = package.CreateRelationship(anchorUri, TargetMode.Internal,
                                                                     "http://schemas.microsoft.com/office/2006/relationships/downRev",
                                                                    "rId2");
                Uri uri = PackUriHelper.ResolvePartUri(rcp.SourceUri, rcp.TargetUri);
                PackagePart part = package.CreatePart(uri, "application/vnd.ms-OpenLS.DrsDownRev+xml", CompressionOption.Maximum);
                using (var stream = part.GetStream())
                {
                    using (TextWriter writer = new StreamWriter(stream, Encoding.UTF8))
                    {
                        writer.Write(DownRevStg.data);
//                        WriteContext context = new WriteContext(SpreadsheetConstants.SpreadsheetDrawingNamespace, writer, part);
  //                      this.DownRevStg.WriteXml(context);
                    }
                }
                
            }
            {
                var anchorUri = new Uri("/drs/picturexml.xml", UriKind.Relative);
                PackageRelationship rcp = package.CreateRelationship(anchorUri, TargetMode.Internal,
                                                                     AnchorWrapper.RelationshipName, "rId1");
                Uri ucp = PackUriHelper.ResolvePartUri(rcp.SourceUri, rcp.TargetUri);
                PackagePart part = package.CreatePart(ucp, AnchorWrapper.ContentType,
                                                      CompressionOption.Maximum);
                using (var stream = part.GetStream())
                {
                    using (XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                    {
                        WriteContext context = new WriteContext(SpreadsheetConstants.SpreadsheetDrawingNamespace, writer,
                                                                part);
                        AnchorWrapper.WriteXml(context);
                    }
                }
            }
        }

        public MetroBlob()
        {
            
        }

        public MetroBlob(Package blob)
        {


            {
                PackageRelationship r =
                    Enumerable.FirstOrDefault(
                        blob.GetRelationshipsByType(
                            AnchorWrapper.RelationshipName));
                if (r != null)
                {
                    PackagePart picturePart =
                        blob.GetPart(PackUriHelper.ResolvePartUri(r.SourceUri, r.TargetUri));
                    Contract.Assert(picturePart.ContentType == AnchorWrapper.ContentType);
                    this.AnchorWrapper = new AnchorWrapper();
                    this.AnchorWrapper.ReadPart(picturePart);
                    var picture2 = this.AnchorWrapper.Item.Content as Picture;
                }
            }
        }


    }
}